Page({
    data: {
      cacheSize: '0B'
    },
  
    onLoad() {
      this.getCacheSize()
    },
  
    // 获取缓存大小
    getCacheSize() {
      wx.getStorageInfo({
        success: res => {
          this.setData({
            cacheSize: this.formatSize(res.currentSize)
          })
        }
      })
    },
  
    // 切换账号
    switchAccount() {
      wx.showModal({
        title: '切换账号',
        content: '确定要切换账号吗？',
        success: res => {
          if (res.confirm) {
            this.logout()
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }
        }
      })
    },
  
    // 清除缓存
    clearCache() {
      wx.showLoading({ title: '清理中' })
      wx.clearStorage({
        success: () => {
          this.getCacheSize()
          wx.hideLoading()
          wx.showToast({ title: '清理完成' })
        }
      })
    },
  
    // 显示退出确认
    showLogoutConfirm() {
      wx.showModal({
        title: '退出登录',
        content: '确定要退出当前账号吗？',
        success: res => {
          if (res.confirm) this.logout()
        }
      })
    },
  
    // 执行退出登录
    logout() {
      wx.removeStorageSync('token')
      wx.removeStorageSync('userInfo')
      wx.reLaunch({
        url: '/pages/login/login'
      })
    },
  
    // 格式化缓存大小
    formatSize(bytes) {
      const units = ['B', 'KB', 'MB', 'GB']
      let unitIndex = 0
      while (bytes >= 1024 && unitIndex < units.length - 1) {
        bytes /= 1024
        unitIndex++
      }
      return bytes.toFixed(2) + units[unitIndex]
    }
  })